<?php

class FormControl extends WBShape {

	const HANDLE_WIDTH = 6;
	const HANDLE_HEIGHT = 6;
	const HANDLE_XOFFSET = 	13;
	const HANDLE_YOFFSET = 	13;

	const HANDLE_XOFFSET_5 = 	15;		// Must be a multiple of the grid
	const HANDLE_YOFFSET_5 = 	15;		// Must be a multiple of the grid

	const NOWHERE = "NOWHERE";
	const INCONTROL = "INCONTROL";
	const INNORTH = "INNORTH";
	const INEAST = "INEAST";
	const INSOUTH = "INSOUTH";
	const INWEST = "INWEST";
	const INNORTHWEST = 	"INNORTHWEST";
	const INNORTHEAST = 	"INNORTHEAST";
	const INSOUTHEAST = 	"INSOUTHEAST";
	const INSOUTHWEST = 	"INSOUTHWEST";		
	
	const TITLEBAR = 0x674F3C;
	const HILITE 	= WHITE;
	const FACE 		= 0xD8DCDC;
	const EDIT 		= WHITE;
	const DISABLED 	= 0xD6D8D8;
	const EDITSHADE = 0xA0A0A0;
	const SHADE 	= 0x788080;
	const SHADOW 	= BLACK;
	const WINDOW 	= 0xE4E2E2;
	const GRIDCOLOR = 0xC8D8D8;
	const OUTLINE 	= 0xFCB0B0;
	const GAUGE 	= 0x804040;
	const HANDLE 	= DARKBLUE;
	const CBREADONLY = 0xEAEFEF;	
	
	private $_isSelected = false;
	private $_distX = 0;
	private $_distY = 0;	
	private $_isMove = false;	
	
	private static $_cursor = array(
		"INCONTROL" 	=> WBUser::IDC_ARROW,
		"NOWHERE" 		=> WBUser::IDC_ARROW,
		"INNORTH" 		=> WBUser::IDC_SIZENS,
		"INSOUTH" 		=> WBUser::IDC_SIZENS,
		"INWEST" 		=> WBUser::IDC_SIZEWE,
		"INEAST" 		=> WBUser::IDC_SIZEWE,
		"INNORTHWEST" 	=> WBUser::IDC_SIZENWSE,
		"INNORTHEAST" 	=> WBUser::IDC_SIZENESW,
		"INSOUTHWEST" 	=> WBUser::IDC_SIZENESW,
		"INSOUTHEAST" 	=> WBUser::IDC_SIZENWSE	
	);
	
	public static $attribute = array(
		'wbtype' 	=> array('type' => 'string', 	'text' => 'Class Name' ),
	
		'name' 		=> array('type' => 'string', 	'text' => 'Name' , 		'editor' => 'editbox' ),
		'title' 	=> array('type' => 'string', 	'text' => 'Title' , 	'editor' => 'editbox' ),
		'caption' 	=> array('type' => 'string', 	'text' => 'Caption' , 	'editor' => 'editbox' ),
		'tooltip' 	=> array('type' => 'string', 	'text' => 'ToolTip' , 	'editor' => 'editbox' ),
		'accel' 	=> array('type' => 'string', 	'text' => 'Accelator' , 'editor' => 'editbox' ),
		'text' 		=> array('type' => 'string', 	'text' => 'Text' , 		'editor' => 'editbox' ),
	
		'icon'		=> array('type' => 'string', 	'text' => 'Icon', 		'editor' => 'open'),
		'image'		=> array('type' => 'string', 	'text' => 'Image', 		'editor' => 'open'),
	
		'layout'	=> array('type' => 'layout', 	'text' => 'Layout', 	'editor' => 'layout' ),
	
		'align' 	=> array('type' => 'string', 	'text' => 'Align', 	'format' => array('left','center', 'right'),	'editor' => 'combobox'),
		'valign' 	=> array('type' => 'string', 	'text' => 'Vertical Align', 	'format' => array('top','middle', 'bottom'),	'editor' => 'combobox'),
	
		'x' 		=> array('type' => 'int', 		'text' => 'x', 		'format' => '%d',	'editor' => 'spinner'),
		'y' 		=> array('type' => 'int', 		'text' => 'y', 		'format' => '%d',	'editor' => 'spinner'),
		'width' 	=> array('type' => 'int', 		'text' => 'width', 	'format' => '%d',	'editor' => 'spinner'),
		'height' 	=> array('type' => 'int', 		'text' => 'height', 'format' => '%d',	'editor' => 'spinner'),
		'current' 	=> array('type' => 'int', 		'text' => 'Current Value', 'format' => '%d',	'editor' => 'spinner'),
		'color' 	=> array('type' => 'int', 		'text' => 'Color', 'format' => '0x%X',	'editor' => 'color'),
		'min' 		=> array('type' => 'int', 		'text' => 'Minimum Value', 'format' => '%d',	'editor' => 'spinner'),
		'max' 		=> array('type' => 'int', 		'text' => 'Maximum Value', 'format' => '%d',	'editor' => 'spinner'),
	
	
		'visible' 	=> array('type' => 'bool', 		'text' => 'Visible', 	'editor' => 'checkbox'),
		'border' 	=> array('type' => 'bool', 		'text' => 'With border', 	'editor' => 'checkbox'),
		'focus' 	=> array('type' => 'bool', 		'text' => 'Focus', 		'editor' => 'checkbox'),
		'disabled' 	=> array('type' => 'bool', 		'text' => 'Disabled', 	'editor' => 'checkbox'),
		'multiline' => array('type' => 'bool', 		'text' => 'Multi-line', 	'editor' => 'checkbox'),
		'checked' 	=> array('type' => 'bool', 		'text' => 'Checked', 	'editor' => 'checkbox'),
		'readOnly' 	=> array('type' => 'bool', 		'text' => 'Read Only', 	'editor' => 'checkbox'),
		'sort' 		=> array('type' => 'bool', 		'text' => 'Sort', 		'editor' => 'checkbox'),
		'number' 	=> array('type' => 'bool', 		'text' => 'Only Number', 	'editor' => 'checkbox'),
		'start' 	=> array('type' => 'bool', 		'text' => 'Start', 	'editor' => 'checkbox'),
		'group' 	=> array('type' => 'bool', 		'text' => 'Group', 	'editor' => 'checkbox'),
		'line' 		=> array('type' => 'bool', 		'text' => 'Line', 	'editor' => 'checkbox'),
		'checkbox' 	=> array('type' => 'bool', 		'text' => 'Checkbox', 	'editor' => 'checkbox'),
		'top' 		=> array('type' => 'bool', 		'text' => 'Always on Top', 	'editor' => 'checkbox'),
		'center' 	=> array('type' => 'bool', 		'text' => 'Centered', 	'editor' => 'checkbox'),
		'ellipsis' 	=> array('type' => 'bool', 		'text' => 'Ellipsis', 	'editor' => 'checkbox'),
		'lines' 	=> array('type' => 'bool', 		'text' => 'Underlined', 	'editor' => 'checkbox'),
		'single' 	=> array('type' => 'bool', 		'text' => 'Single Select', 	'editor' => 'checkbox'),
		'mask' 		=> array('type' => 'bool', 	'text' => 'Mask Char' , 'editor' => 'checkbox' ),	
	);
	
	public static $checkboxControl = array(
		'visible',
		'border',
		'focus',
		'disabled',
		'multiline', 
		'checked', 
		'readOnly',
		'sort', 
		'number',
		'start', 
		'group', 
		'line', 	
		'checkbox',
		'top',
		'center',
		'ellipsis',
		'lines',
		'single',
		'mask'
	);
	
	protected static $realFont = null; 
	protected static $disabledFont = null; 	
	protected static $lfarrow = null;
	protected static $rtarrow = null;
	protected static $uparrow = null;
	protected static $dnarrow = null;
	protected static $calendar = null;
	protected static $imagebutton = null;
	protected static $draw_radio = null;
	protected static $draw_radio_checked = null;
	protected static $draw_checkbox = null;
	protected static $draw_checkbox_checked = null;	
	protected static $draw_treeview_lines = null;
	protected static $draw_treeview = null;
	protected static $draw_htmlcontrol = null;
	
	private $_treeNode = null;
	private $_control = array();
	
	public function __construct($options) { 
		parent::__construct($options);
		
		self::initResource();
	}
	
	public function initShape() { 
		parent::initShape();
		
		if (!isset($this->visible)) 	$this['visible'] = true;
		if (!isset($this->disabled)) 	$this['disabled'] = false;
		if (!isset($this->focus)) 		$this['focus'] = false;
		if (!isset($this->multiline)) 	$this['multiline'] = false;
		if (!isset($this->checked)) 	$this['checked'] = false;
		if (!isset($this->sort)) 		$this['sort'] = false;
		if (!isset($this->number)) 		$this['number'] = false;
		if (!isset($this->start)) 		$this['start'] = false;
		if (!isset($this->group)) 		$this['group'] = false;
		if (!isset($this->line)) 		$this['line'] = false;
		if (!isset($this->checkbox)) 	$this['checkbox'] = false;
		if (!isset($this->top)) 		$this['top'] = false;
		if (!isset($this->center)) 		$this['center'] = false;
		if (!isset($this->ellipsis))	$this['ellipsis'] = false;
		if (!isset($this->lines)) 		$this['lines'] = false;
		if (!isset($this->single)) 		$this['single'] = false;
		if (!isset($this->mask)) 		$this['mask'] = false;
		if (!isset($this->style)) 		$this['style'] = 0;
		if (!isset($this->autoVisible)) $this['autoVisible'] = true;
		if (!isset($this->children)) 	$this['children'] = array();
		if (isset($this->treeNode)) 	$this->setTreeNode($this['treeNode']);
	}
	
	public static function initResource() { 
		$window = ProjectManager::$mainWindow;
		
		$window->setResource('realFont', 'font', array("Tahoma", 8, BLACK, 'normal'));
		$window->setResource('disabledFont', 'font', array("Tahoma", 8, 0xA0A0A0, 'normal'));
		
		$window->setResource('symb_calendar', 'image', RESOURCE_HOME . "symb_calendar.bmp");
		$window->setResource('symb_imagebutton', 'image', RESOURCE_HOME."symb_imagebutton.bmp");
		$window->setResource('draw_checkbox', 'image', RESOURCE_HOME . "draw_checkbox.bmp");
		$window->setResource('draw_checkbox_checked', 'image', RESOURCE_HOME . "draw_checkbox_checked.bmp");
		$window->setResource('draw_radio', 'image', RESOURCE_HOME . "draw_radio.bmp");
		$window->setResource('draw_radio_checked', 'image', RESOURCE_HOME . "draw_radio_checked.bmp");		
		$window->setResource('symb_lfarrow', 'image', RESOURCE_HOME . "symb_lfarrow.bmp");
		$window->setResource('symb_rtarrow', 'image', RESOURCE_HOME . "symb_rtarrow.bmp");
		$window->setResource('symb_uparrow', 'image', RESOURCE_HOME . "symb_uparrow.bmp");
		$window->setResource('symb_dnarrow', 'image', RESOURCE_HOME . "symb_dnarrow.bmp");
		$window->setResource('draw_treeview_lines', 'image', RESOURCE_HOME."draw_treeview_lines.bmp");
		$window->setResource('draw_treeview', 'image',  RESOURCE_HOME."draw_treeview.bmp");
		$window->setResource('draw_htmlcontrol', 'image',  RESOURCE_HOME."draw_htmlcontrol.bmp");
				
		self::$realFont 	= $window->getResource('realFont');
		self::$disabledFont = $window->getResource('disabledFont');
		
		self::$lfarrow 		= $window->getResource('symb_lfarrow');
		self::$rtarrow 		= $window->getResource('symb_rtarrow');
		self::$uparrow 		= $window->getResource('symb_uparrow');
		self::$dnarrow 		= $window->getResource('symb_dnarrow');
		self::$calendar 	= $window->getResource('symb_calendar');
		self::$imagebutton 	= $window->getResource('symb_imagebutton');
		self::$draw_radio 	= $window->getResource('draw_radio');
		self::$draw_radio_checked 	= $window->getResource('draw_radio_checked');
		self::$draw_checkbox 	= $window->getResource('draw_checkbox');
		self::$draw_checkbox_checked 	= $window->getResource('draw_checkbox_checked');		
		self::$draw_treeview_lines 	= $window->getResource('draw_treeview_lines');
		self::$draw_treeview 	= $window->getResource('draw_treeview');
		self::$draw_htmlcontrol 	= $window->getResource('draw_htmlcontrol');
	}
	
	public function setTreeNode($node) { 
		$this->_treeNode = $node;
	}
	
	public function getTreeNode() { 
		return $this->_treeNode;
	}
	
	public function createControl($type, $options = array()) { 
		$obj = ProjectManager::getControlTemplate($type, $options);
		
		$this->add($obj);  
		
		return $obj;
	}	
	
	public function add($control) { 
		$this->_control[] = $control;
	}
	
	public function remove($i) { 
		unset($this->_control[$i]);
		
		$this->_control = array_merge(array(), $this->_control);
	}
	
	public function removeById($id) {
		$temp = -1;
		
		foreach ($this->_control as $i => $control) { 
			if ($control['id'] == $id) {
				$temp = $i; 
				break;
			}
		}
		
		if ($temp > -1) { 
			$this->remove($temp);
		}
	}	
	
	public function length() { 
		return sizeof($this->_control);
	}	
	
	public function get($i) { 
		return $this->_control[$i];
	}
	
	public function getComponentList() { 
		return $this->_control;
	}
	
	public function initData() {
		$this->_isMove = false;
		$this->_distX = 0;
		$this->_distY = 0;
		//$this['handle'] = self::NOWHERE;
	}	
	
	public function setSelected($isSelected = false) { 
		$this->_isSelected = $isSelected;
	}
	
	public function isSelected() { 
		return ($this->_isSelected === true);
	}
	
	
	public function setMousePosition($x, $y) {
		if (!$this->_isMove) { 
			$this->_distX = $x - $this->getOption('x');
			$this->_distY = $y - $this->getOption('y');
			
			$this->_isMove = true;
		}
	}	
	
	public function setMovePosition($x, $y) {
		$dx = $x - $this->_distX;
		$dy = $y - $this->_distY;
		
		$grid = ProjectManager::$checkSnapInGrid->isChecked();

		if ($grid) $dx = (int)(max(4, $dx)/5)*5;
		if ($grid) $dy = (int)(max(4, $dy)/5)*5;
		
		$this->moveTo($dx, $dy);
	}	
	
	public function setScale($eo) {
		
		$handle = $this->getOption('handle');
		$eo->window()->setCursor(self::$_cursor[$handle]);
		$y = $eo->getY();
		$x = $eo->getX();
					
		if ($handle == self::INNORTH) {  // up , down 

			$newY = $y;
			
			$bottom = $this->getOption('y') + $this->getOption('height');
			$newtop = $newY + self::HANDLE_YOFFSET_5;
			$newheight = $bottom - $newtop;
			
			if ($newheight > 0) { 
				$this['y'] = $newtop;
				$this['height'] = $newheight;	
			}
		} else if ($handle == self::INSOUTH) {  // down, up 
			$newY = $y;
			
			$top = $this->getOption('y');
			$newbottom = $newY - self::HANDLE_YOFFSET_5;
			$newheight = $newbottom - $top;
			
			if ($newheight > 0) { 
				$this['height'] = $newheight;	
			}			
		} else if ($handle == self::INWEST) {  // left, right 
			$newX = $x;
			
			$right = $this->getOption('x') + $this->getOption('width');
			$newLeft = $newX - self::HANDLE_XOFFSET_5;
			$newwidth = $right - $newLeft;
			
			if ($newwidth > 0) { 
				$this['x'] = $newLeft;	
				$this['width'] = $newwidth;	
			}			
		} else if ($handle == self::INEAST) {    // right, left 
			$newX = $x;
			
			$left = $this->getOption('x');
			$newRight = $newX - self::HANDLE_XOFFSET_5;
			$newwidth = $newRight - $left;
			
			if ($newwidth > 0) { 
				$this['width'] = $newwidth;	
			}			
		} else if ($handle == self::INNORTHEAST) {    // right up
			$newX = $x;
			$newY = $y;
			
			$left = $this->getOption('x');
			$bottom = $this->getOption('y') + $this->getOption('height');
			
			$newLeft = $newX - self::HANDLE_XOFFSET_5;
			$newWidth = $newLeft - $left;
			
			if ($newWidth > 0) { 
				$this['width'] = $newWidth;	
			}			
			
			$newTop = $newY - self::HANDLE_YOFFSET_5;
			$newHeight = $bottom - $newTop;
			
			if ($newHeight > 0) { 
				$this['y'] = $newTop;
				$this['height'] = $newHeight;
			}
		} else if ($handle == self::INSOUTHEAST) {  // right down
			$newX = $x;
			$newY = $y;
			
			$left = $this->getOption('x');
			$top = $this->getOption('y');
			
			$newLeft = $newX - self::HANDLE_XOFFSET_5;
			$newWidth = $newLeft - $left;
			
			if ($newWidth > 0) { 
				$this['width'] = $newWidth;	
			}			
			
			$newTop = $newY - self::HANDLE_YOFFSET_5;
			$newHeight = $newTop - $top;
			
			if ($newHeight > 0) { 
				$this['height'] = $newHeight;
			}
		} else if ($handle == self::INNORTHWEST) {  // left up
			$newX = $x;
			$newY = $y;
			
			$right = $this->getOption('x') + $this->getOption('width');
			$bottom = $this->getOption('y') + $this->getOption('height');
			
			$newLeft = $newX + self::HANDLE_XOFFSET_5;
			$newWidth = $right - $newLeft;
			
			if ($newWidth > 0) {
				$this['x'] = $newLeft; 
				$this['width'] = $newWidth;	
			}			
			
			$newTop = $newY - self::HANDLE_YOFFSET_5;
			$newHeight = $bottom - $newTop;
			
			if ($newHeight > 0) { 
				$this['y'] = $newTop;
				$this['height'] = $newHeight;
			}
		} else if ($handle == self::INSOUTHWEST) {  // left down
			$newX = $x;
			$newY = $y;
			
			$right = $this->getOption('x') + $this->getOption('width');
			$top = $this->getOption('y');
			
			$newLeft = $newX + self::HANDLE_XOFFSET_5;
			$newWidth = $right - $newLeft;
			
			if ($newWidth > 0) {
				$this['x'] = $newLeft; 
				$this['width'] = $newWidth;	
			}			
			
			$newBottom = $newY - self::HANDLE_YOFFSET_5;
			$newHeight = $newBottom - $top;
			
			if ($newHeight > 0) { 
				$this['height'] = $newHeight;
			}
		}
		
	}
	
	/**
	 * 선택되어진 상태 그리기 
	 * 
	 * @param WBGraphics $graphics
	 */
	public function displayForSelected(WBGraphics $g) {

		$xstart = $this->getOption('x');
		$ystart = $this->getOption('y');
		$width = $this->getOption('width');
		$height = $this->getOption('height');
		$xcenter = $xstart + $width / 2;
		$xend = $xstart + $width;
		$ymiddle = $ystart + $height / 2;
		$yend = $ystart + $height;		
		
		// top 
		$g->rect($xstart - self::HANDLE_XOFFSET, $ystart - self::HANDLE_YOFFSET, self::HANDLE_WIDTH, self::HANDLE_HEIGHT, BLACK, true);				
		$g->rect($xcenter - self::HANDLE_WIDTH / 2, $ystart - self::HANDLE_YOFFSET, self::HANDLE_WIDTH, self::HANDLE_HEIGHT, BLACK, true);				
		$g->rect($xend + self::HANDLE_XOFFSET - self::HANDLE_WIDTH, $ystart - self::HANDLE_YOFFSET, self::HANDLE_WIDTH, self::HANDLE_HEIGHT, BLACK, true);
		 				
		// middle
		$g->rect($xstart - self::HANDLE_XOFFSET, $ymiddle - self::HANDLE_HEIGHT/2, self::HANDLE_WIDTH, self::HANDLE_HEIGHT, BLACK, true);				
		$g->rect($xend + self::HANDLE_XOFFSET - self::HANDLE_WIDTH, $ymiddle - self::HANDLE_HEIGHT/2, self::HANDLE_WIDTH, self::HANDLE_HEIGHT, BLACK, true);
		 
		// bottom 
		$g->rect($xstart - self::HANDLE_XOFFSET, $yend + self::HANDLE_YOFFSET - self::HANDLE_HEIGHT, self::HANDLE_WIDTH, self::HANDLE_HEIGHT, BLACK, true);				
		$g->rect($xcenter - self::HANDLE_WIDTH / 2, $yend + self::HANDLE_YOFFSET - self::HANDLE_HEIGHT, self::HANDLE_WIDTH, self::HANDLE_HEIGHT, BLACK, true);				
		$g->rect($xend + self::HANDLE_XOFFSET - self::HANDLE_WIDTH, $yend + self::HANDLE_YOFFSET - self::HANDLE_HEIGHT, self::HANDLE_WIDTH, self::HANDLE_HEIGHT, BLACK, true);

		// outline (dashed)
		$g->rect($xstart, $ystart, $width, $height, BLACK, false, 0, WBGraphics::LINE_DASHES_3);
	}
	
	public function getHandleArea($x, $y) { 
		
		$xstart = $this->getOption('x');
		$ystart = $this->getOption('y');
		$width = $this->getOption('width');
		$height = $this->getOption('height');		
		$xcenter = $xstart + $width / 2;
		$xend = $xstart + $width;
		$ymiddle = $ystart + $height / 2;
		$yend = $ystart + $height;				
	
		if ($this->isArea($x, $y)) { 
			return self::INCONTROL;
		} else if ( ($this['x'] - self::HANDLE_XOFFSET <= $x  && $x <= $xstart) && ($ystart - self::HANDLE_YOFFSET <= $y && $y <= $ystart) ) { 
			return self::INNORTHWEST;
		} else if ( ($xcenter - self::HANDLE_WIDTH / 2 <= $x  && $x <= $xcenter + self::HANDLE_WIDTH / 2) && ($ystart - self::HANDLE_YOFFSET <= $y && $y <= $ystart) ) { 
			return self::INNORTH;
		} else if ( ($xend + self::HANDLE_XOFFSET - self::HANDLE_WIDTH <= $x  && $x <= $xend + self::HANDLE_XOFFSET) && ($ystart - self::HANDLE_YOFFSET <= $y && $y <= $ystart) ) { 
			return self::INNORTHEAST;
		} else if ( ($xstart - self::HANDLE_XOFFSET <= $x  && $x <= $xstart) && ($ymiddle - self::HANDLE_HEIGHT/2 <= $y && $y <= $ymiddle + self::HANDLE_HEIGHT/2) ) { 
			return self::INWEST;
		} else if ( ($xend + self::HANDLE_XOFFSET - self::HANDLE_WIDTH <= $x  && $x <= $xend + self::HANDLE_XOFFSET) && ($ymiddle - self::HANDLE_HEIGHT/2 <= $y && $y <= $ymiddle + self::HANDLE_HEIGHT/2) ) { 
			return self::INEAST;
		} else if ( ($xstart - self::HANDLE_XOFFSET <= $x  && $x <= $xstart) && ($yend + self::HANDLE_YOFFSET - self::HANDLE_HEIGHT <= $y && $y <= $yend + self::HANDLE_YOFFSET) ) { 
			return self::INSOUTHWEST;
		} else if ( ($xcenter - self::HANDLE_WIDTH / 2 <= $x  && $x <= $xcenter + self::HANDLE_WIDTH / 2) && ($yend + self::HANDLE_YOFFSET - self::HANDLE_HEIGHT <= $y && $y <= $yend + self::HANDLE_YOFFSET) ) { 
			return self::INSOUTH;
		} else if ( ($xend + self::HANDLE_XOFFSET - self::HANDLE_WIDTH <= $x  && $x <= $xend + self::HANDLE_XOFFSET) && ($yend + self::HANDLE_YOFFSET - self::HANDLE_HEIGHT <= $y && $y <= $yend + self::HANDLE_YOFFSET) ) { 
			return self::INSOUTHEAST;
		} else { 
			return self::NOWHERE;
		}
	}
	
	public function setMouseCursor(WBEvent $eo) { 
		$handle = $this->getHandleArea($eo->getX(), $eo->getY());
		
		// set mouse handle 
		$this['handle'] = $handle;
		
		if ($handle != self::NOWHERE) { 
			$eo->window()->setCursor(WBUser::IDC_ARROW);
		}
	}
	
	/**
	 * render control  
	 * 
	 * @param WBGraphics $graphics
	 */
	public function displayControl($graphics) { 
		
	}
	
	public function display($graphics) {
	 
		$this->displayControl($graphics);
		
		$this->doLayout();
		
		$this->displayChildrenControl($graphics);
		
	}
	
	public function doLayout() { 
		$layout = $this['layout'];		

		if ($layout) { 
			
			if (is_string($layout) || is_array($layout)) { 
				$this['layout'] = wblayout($layout);
			}
			if (is_object($this['layout'])) { 
				$this['layout']->render($this);
			}
		}
	}
	
	public function displayChildrenControl($g) { 
		$len = $this->length();
		for($i = 0; $i < $len; $i++) { 
			$this->get($i)->display($g);
		}
	}
	
	
	public function displayButton(WBGraphics $g, $caption, $xstart, $ystart, $width, $height) {

		$xend = $xstart + $width;
		$yend = $ystart + $height;
		
		$g->rect($xstart, $ystart, $width, $height, FormControl::FACE, true);
		$g->line($xend - 2, $ystart, $xend - 2, $yend - 2, FormControl::SHADE);
		$g->line($xstart, $yend - 2, $xend - 2, $yend - 2, FormControl::SHADE);
		$g->line($xstart, $ystart, $xend - 2, $ystart, FormControl::HILITE);
		$g->line($xstart, $ystart,$xstart, $yend - 2, FormControl::HILITE);
		$g->line($xend - 1, $ystart, $xend - 1, $yend - 1, FormControl::SHADOW);
		$g->line($xstart, $yend - 1, $xend - 2, $yend - 1, FormControl::SHADOW);
		$g->text($caption,$xstart, $ystart, $width, $height, $font, WBC_CENTER | WBC_MIDDLE);
	}		
	
	public function checkStyle($constant) { 
		return ($this['style'] & wbconstant($constant));
	}
	
	public function addStyle($constant) { 
		$this['style'] |= wbconstant($constant);
	}

	public function checkPoint($x, $y) {

		$control = $this->checkControlInArea($x, $y);
		
		if ($control) { 
			return $control;			
		} else { 
			if ($this->isArea($x, $y)) { 
				return $this;	
			}
		}
		
		return null;
		
	}
	
	public function checkControlInArea($x, $y) { 
		
		$len = $this->length();
		
		for($i = 0; $i < $len; $i++) {
			$control = $this->get($i);
			
			$realControl = $control->checkControlInArea($x, $y);
			 
			if($realControl) { 
				return $realControl;
			} 
		}
		
		if ($this->isArea($x, $y)) { 
			return $this;
		}		
		
		return null;
	}
	
	public function getProperty() { 
		return array();
	}
	
	public function toData() {
		
		$temp = array('dataType' => 'control');
		
		foreach ($this->getProperty() as $type) {
			$obj = self::$attribute[$type];
			$value = 0;
			
			if ($obj['type'] == 'string') { 
				$value = $this[$type];
			} else if ($obj['type'] == 'int') { 
				$value = sprintf($obj['format'], $this[$type]);
			} else if ($obj['type'] == 'bool') { 
				$value = (bool)$this[$type];
			}
			
			$temp[$type] = array('type' => $type, 'text' => $obj['text'], 'editor' => $obj['editor'], 'value' => $value);
		}
		
		return $temp;
	}
	
	public function toPropertyList() {
		return $this->toData();
	}
	
	public function update($data) { 
		foreach ($data as $key => $value) { 
			$this->setOption($key, $value);
		}
	}
	
	public function getXmlData() { 
		return $this->getProperty();
	}
	
	public function getXmlList() { 
		$data = array();

		foreach ($this->getXmlData() as $key ) { 
			$data[$key] = $this[$key];
		}
			
		return $data;
	}
	
	public function toXML() { 
		
		$root = x_('control');
		
		$options = x_('options');
		
		foreach ($this->getXmlList() as $key => $value) { 
			$options->{$key}($value);
		}	
				
		$root->add($options);
		
		$controls = x_('controls');
		
		$list = (array)$this->getComponentList();
		foreach ($list as $con) {
			$controls->add($con->toXML());
		}	

		$root->add($controls);
		
		return $root->toString();			

	}
}


?>